<?php

namespace App\Modules\Admin\Services;

use App\Modules\Admin\Repositories\Contracts\ActivityFormRepository;
use App\Modules\Admin\Repositories\Transformers\ActivityFormTransformer;

class ActivityFormService
{
    private $activityFormRepository;

    public function __construct(ActivityFormRepository $activityFormRepository)
    {
        $this->activityFormRepository = $activityFormRepository;
    }

    /**
     * 获取列表
     *
     * @param array|null $param
     */
    public function getList($param = null)
    {
        try {
            $filter = [];
            if (isset($param['activity_id']) && $param['activity_id'] != 0) {
                $filter['activity_id'] = $param['activity_id'];
            }
            if (isset($param['phone']) && $param['phone'] != '') {
                $filter['phone'] = $param['phone'];
            }
            if (isset($param['audit_status'])) {
                $filter['audit_status'] = $param['audit_status'];
            }

            $activityForm = $this->activityFormRepository->paginate($param['per_page'] ?? null, $filter);
            $result = fractal($activityForm, new ActivityFormTransformer());

            return $result->toArray();

        } catch (\Throwable $th) {
            throw $th;
        }
    }

    /**
     * 设置审核状态
     */
    public function setAuditStatus($param)
    {
        try {
            $data = [
                'audit_status' => $param['audit_status'],
            ];

            $activity = $this->activityFormRepository->updateByWhereIn($data, [
                'column' => 'id',
                'data' => $param['ids'],
            ]);
            return $activity;

        } catch (\Throwable $th) {
            throw $th;
        }
    }
}
